package Client;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ServerApp.IServer;
import ServerApp.Tools;
import ServerApp.User;

public class NewUser extends JFrame {

	private IServer server;
	private JLabel lTitle = new JLabel("New User", JLabel.CENTER);
	private JLabel lName = new JLabel("Name: ");
	private JTextField tName = new JTextField(20);
	private JLabel lPassword = new JLabel("Password: ");
	private JTextField tPassword = new JTextField(20);
	private JPanel pnPermission = new JPanel();
	private JLabel lPermission = new JLabel("Permission to create meetings");
	private JCheckBox cbPermission = new JCheckBox();
	private JButton bnOK = new JButton("  OK  ");
	private JButton bnCancel = new JButton("Cancel");
	private JPanel pnButtons = new JPanel();
	private User currentUser;
	
	public NewUser(IServer server, User user) {

		this.server = server;
		this.currentUser = user;
		lTitle.setFont(new Font("SansSerif", Font.BOLD, 16));

		bnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				bnOkAction();
			}
		});

		bnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bnCancelAction();
			}
		});

		pnButtons.add(bnOK);
		pnButtons.add(bnCancel);

		pnPermission.add(lPermission);
		pnPermission.add(cbPermission);

		this.getContentPane().setLayout(new GridBagLayout());

		this.getContentPane().add(
				lTitle,
				new GridBagConstraints(0, 0, 2, 1, 1.0, 1.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));
		this.getContentPane().add(
				lName,
				new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 6, 3, 3), 0, 0));
		this.getContentPane().add(
				tName,
				new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 3, 3, 6), 0, 0));
		this.getContentPane().add(
				lPassword,
				new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 6, 3, 3), 0, 0));
		this.getContentPane().add(
				tPassword,
				new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(3, 3, 3, 6), 0, 0));
		this.getContentPane().add(
				pnPermission,
				new GridBagConstraints(0, 3, 2, 1, 1.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.NONE,
						new Insets(3, 1, 3, 3), 0, 0));
		this.getContentPane().add(
				pnButtons,
				new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.NONE,
						new Insets(3, 6, 3, 3), 0, 0));
//		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.pack();
		// this.setSize(400, 300);
		this.setResizable(false);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
				/ 2 - this.getHeight() / 2);
	}

	protected void bnOkAction() {
		String errorText = "";
		if (tName.getText().isEmpty())
			errorText += "Enter valid user name\n";
		if (tPassword.getText().isEmpty())
			errorText += "Enter valid password\n";
		
		if (errorText.isEmpty()) {
			try {
				if(!server.addNewUser(new User(tName
						.getText(), tPassword.getText(), cbPermission
						.isSelected()), currentUser)){
					Tools.errorMessage("Timeout", "Your session has timed out. please login again.");
					server.logOut(currentUser);
					this.dispose();
					new LoginFenster().setVisible(true);
				}
			} catch (RemoteException e) {
				e.printStackTrace();
				errorText += e.getMessage();
			}
			if (!errorText.isEmpty())
				Tools.errorMessage("Error", errorText);
		}
		else
			Tools.errorMessage("Error", errorText);
		
		this.dispose();		
	}

	protected void bnCancelAction() {
		this.dispose();
	}

	// public static void main(String[] args) {
	// new NewUser().setVisible(true);
	// }
}
